package cz.muni.fi.pa165.pokemon.service.impl;

import cz.muni.fi.pa165.pokemon.dao.TrainerDAO;
import cz.muni.fi.pa165.pokemon.dto.TrainerTO;
import cz.muni.fi.pa165.pokemon.entity.Trainer;
import cz.muni.fi.pa165.pokemon.service.TrainerService;
import java.util.ArrayList;
import java.util.List;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Author: Michal Toth Date: 10/31/12 Time: 1:31 PM
 */
@Service
public class TrainerServiceImpl implements TrainerService {

    @Autowired
    private TrainerDAO trainerDAO;
    @Autowired
    private DozerBeanMapper mapper;

    @Transactional
    @Override
    public void create(TrainerTO trainer) {
        if (trainer == null) {
            throw new IllegalArgumentException("trainer is null");
        }
        
        if (trainer.getId() != null) {
            throw new IllegalArgumentException("trainer.id is not null");
        }
        Trainer tm = mapper.map(trainer, Trainer.class);
        trainerDAO.create(tm);
        trainer.setId(tm.getId());
    }

    @Transactional(readOnly = true)
    @Override
    public TrainerTO get(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("Trainer.Id is null");
        }
        
        Trainer trainer = trainerDAO.get(id);
        if (trainer == null) {
            return null;
        }        
        return mapper.map(trainerDAO.get(id), TrainerTO.class);
    }

    @Transactional
    @Override
    public void update(TrainerTO trainer) {
        if (trainer == null) {
            throw new IllegalArgumentException("Trainer is null");
        }        
        if (trainer.getId() == null) {
            throw new IllegalArgumentException("Trainer.id is null");
        }
        Trainer tm = mapper.map(trainer, Trainer.class);
        trainerDAO.update(tm);
    }

    @Transactional
    @Override
    public void remove(TrainerTO trainer) {
        if (trainer == null) {
            throw new IllegalArgumentException("Trainer is null");
        }        
        if (trainer.getId() == null) {
            throw new IllegalArgumentException("Trainer.id is null");
        }
        Trainer tm = mapper.map(trainer, Trainer.class);
        trainerDAO.remove(tm);
    }

    @Transactional(readOnly= true)
    @Override
    public List<TrainerTO> findAll() {
        List<Trainer> result = trainerDAO.findAll();
        List<TrainerTO> trainerTOList = new ArrayList<>();
        
        for (Trainer t : result) {
            trainerTOList.add(mapper.map(t, TrainerTO.class));
        }
        return trainerTOList;
    }

    @Transactional(readOnly= true)
    @Override
    public List<TrainerTO> findByName(String name) {
        if (name == null) {
            throw new IllegalArgumentException("null name");
        }       
        
        List<Trainer> result = trainerDAO.findByName(name);
        List<TrainerTO> trainerTOList = new ArrayList<>();
        
        for (Trainer t : result) {
            trainerTOList.add(mapper.map(t, TrainerTO.class));
        }
        return trainerTOList;
    }
}
